// pages/nextstep/nextstep.js
const {
  get,
  post,
  postList
} = require('../../request/index')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: {},
    prolist: wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist')),
    hotList: [
      {
        label: '动火环境检测人员',
        prop: 'auditMemberId',
        type: 'multiDropDown',
        sort:1,
         net: {
         url: '/app/asysrole/getAllMember',
         method: 'post',
         data: {
           mineId: true,
         },
         key: 'memberId',
         value: 'nickName'
       }
      },
      {
        label: '安全措施确认人',
        prop: 'auditMemberId',
        sort: 2,
        net: {
          url: '/app/asysrole/getAllMember',
          method: 'post',
          data: {
            mineId: true,
          },
          key: 'memberId',
          value: 'nickName'
        }
      },
      {
        label: '动火单位',
        prop: 'auditMemberId',
        sort: 3,
        net: {
          url: '/app/asysrole/getAllMember',
          method: 'post',
          data: {
            mineId: true,
          },
          key: 'memberId',
          value: 'nickName'
        }
      },
   
      {
        label: '动火单位监护人员',
        prop: 'auditMemberId',
        sort: 7,
        net: {
          url: '/app/asysrole/getAllMember',
          method: 'post',
          data: {
            mineId: true,
          },
          key: 'memberId',
          value: 'nickName'
        }
      },

    ],
    radioList:[ 
      {
        label: '安全管理部门',
        prop: 'auditMemberId',
        sort: 4,
        radio:"",
        list: [{
            userId: '4345',
            name: "吴华"
          },
          {
            userId: '4551',
            name: "饶钢"
          }
        ]
      },
      {
        label: '生产部门',
        prop: 'auditMemberId',
        sort: 5,
        radio:"",
        list: [{
          userId: '1',
          name: "廖大勇"
        },
        {
          userId: '2',
          name: "陈海"
        }
      ]
      },
      {
        label: '矿领导审批',
        prop: 'auditMemberId',
        sort: 6,
        radio:"",
        list:[
          {userId:'4344',
            name:'张威'}
        ]
      },
    ],
    
    narr: [],
    flowId: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    let {
      url,
      data
    } = options
 if( url,data){
  this.getCode(url, JSON.parse(data))
 }
   

  },

  onChange(e){
      
      let {radioList, narr} = this.data;
      let obj = {}
      let arr = []
     let list =  radioList.map(item=>{
        if(item.sort==e.currentTarget.dataset.item.sort){
           return {...item,radio:e.detail}
        }else{
          return{...item}
        }
      })
        
      obj[e.target.dataset.item.prop] = e.detail
      obj.sort = e.target.dataset.item.sort
      obj.flowId = this.data.flowId;
      
      arr.push(obj)
      this.setData({
        radioList:list,
        narr: narr.concat(arr)
      })
     
  },

  getCode(url, data) {
    let {
      prolist
    } = this.data
    post(url, {
      ...data,
      addMemberId: prolist.userid,
      mineId: prolist.mineId
    }).then(res => {
      this.setData({
        flowId: res.msg
      })
    })

  },
  change(e) {
    console.log(e);
    let a = this.data.data
    let {
      narr
    } = this.data
    let obj = {}
    obj[e.target.dataset.item.prop] = e.detail
    obj.sort = e.target.dataset.item.sort
    obj.flowId = this.data.flowId;

    let arr = []

    arr.push(obj)

    this.setData({
      narr: narr.concat(arr)
    })



  },

  sbtmit() {
    let {
      narr
    } = this.data
    let nArr = narr.filter((currentValue, currentIndex, selfArr) => {
      return selfArr.findIndex(x => x.sort === currentValue.sort) === currentIndex
    });
    this.setData({
      narr: nArr
    })
    console.log(nArr);
    // let f = JSON.stringify(nArr)
    console.log(this.data.narr.length);

    if (this.data.narr.length == 7) {
      post('/app/flowDetail/insert', [...nArr]).then(res => {
        console.log(res);
        if (res.success) {
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 1500,
            mask: true
          })
          wx.navigateBack({
            delta: 2
          })
        }
      })
    } else {
      wx.showToast({
        title: '提交失败',
        icon: 'success',
        duration: 1500,
        mask: true
      })
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})